// components/checkboxgroup.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    checkstitle: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    tags: [{ key: 'key1', name: 'tag1',selected:1 }, { key: 'key2', name: 'tag2'}],
    countries:[],
    itemsize:0,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    set_data: function ({ tags, itemsize} = {}) {
      let data = {}
      if (tags !== undefined) {
        data['tags'] = tags
      }
      if (itemsize !== undefined) {
        data['itemsize'] = itemsize
      }
      this.setData(data)
    },
    event: function (event) {
      this.triggerEvent(event, this.data.countries, {})
    },
    onCheckboxChange: function (e) {
      var thisdata = this.data
      var checkid = e.currentTarget.id;
      var selected = e.currentTarget.dataset.selected;
      thisdata.tags.forEach(function(item){
        if (item.key === checkid){
            item['selected'] = 1^selected;//异或运算
          }
        if (item['selected'] === 1){
          thisdata.countries.push(item.key);
        }
      })

      this.setData(thisdata)
      this.event('change')
    }
  }
})
